package com.wholeintech.ble;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;

import com.wholeintech.utils.CommonUtil;
import com.wholeintech.entity.DeviceDb;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.UUID;
import java.util.Vector;

import static com.wholeintech.ble.DeviceList.TYPE_LIGHT;

/**
 * Created by Lyle on 2016-04-20.
 */
public class YeeLightDev {

    private String TAG = "YeeLight";

    private static final UUID YEELIGHT_SERVICE_UUID = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");
    private static final UUID YEELIGHT_COLOR_UUID = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb");

    private int type = TYPE_LIGHT;

    private boolean onLineState = false;

    Vector<DevInfoItem> mDevInfoItem;

    private Context mContext;

    private YeeLightDev() {
    }

    public YeeLightDev(Context c, Vector<DevInfoItem> d) {
        mDevInfoItem = d;
        mContext = c;
    }

    private synchronized void writeRXCharacteristic(BluetoothGatt mBtGatt, byte[] value) {
        BluetoothGattService RxService = mBtGatt.getService(YEELIGHT_SERVICE_UUID);

        if (RxService == null) {
            Log.e(TAG, "Rx service not found!");
            return;
        }
        BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(YEELIGHT_COLOR_UUID);
        if (RxChar == null) {
            Log.e(TAG, "Rx characteristic not found!");
            return;
        }
        RxChar.setValue(value);
        boolean status = mBtGatt.writeCharacteristic(RxChar);
        if (status == false) {
            //mBtGatt.discoverServices();
        }

        Log.i(TAG, "writeRXCharacteristic status:" + status);
    }

    public void setYeeColorBright(String address, int color, int brightness) {

        for (int i = 0; i < mDevInfoItem.size(); i++) {
            if (address.equals(mDevInfoItem.get(i).getDevMAC())) {
                if (mDevInfoItem.get(i).getBtGatt() != null) {
                    int b = color & 0x000000ff;
                    int g = (color & 0x0000ff00) >> 8;
                    int r = (color & 0x00ff0000) >> 16;

                    Log.i(TAG, "setYeeColorBright:" + "R:" + r + ",G:" + g + ",B:" + b + ",Brig:" + brightness);

                    String s = String.format("%d,%d,%d,%d,,,,,,,,,,,,,,", r, g, b, brightness);

                    writeRXCharacteristic(mDevInfoItem.get(i).getBtGatt(), s.substring(0, 18).getBytes());
                }

                break;
            }
        }

        DbManager db = CommonUtil.MyDbUtils();
        try {

//            DeviceDb me = db.findFirst(Selector.from(DeviceDb.class).where("macAddr", "=", address));
            DeviceDb me = db.selector(DeviceDb.class).where("macAddr", "=", address).findFirst();
            if (me != null) {
                me.setBrightness(brightness);
                me.setColor(color);
                db.saveOrUpdate(me);
            } else {
                Log.i(TAG, "setYeeColorBright: Not found, MAC:" + address);
            }

        } catch (DbException | NumberFormatException e) {

            e.printStackTrace();

        }
    }

    public void setYeeBright(String address, int brightness) {

        for (int i = 0; i < mDevInfoItem.size(); i++) {
            if (address.equals(mDevInfoItem.get(i).getDevMAC())) {
                if (mDevInfoItem.get(i).getBtGatt() != null) {
                    String s = String.format(",,,%d,,,,,,,,,,,,,,,,,,,,,", brightness > 100 ? 100 : brightness);
                    writeRXCharacteristic(mDevInfoItem.get(i).getBtGatt(), s.substring(0, 18).getBytes());
                    Log.i(TAG, "setYeeBright:" + brightness);
                }
                break;
            }
        }

        DbManager db = CommonUtil.MyDbUtils();
        try {

//            DeviceDb me = db.findFirst(Selector.from(DeviceDb.class).where("macAddr", "=", address));
            DeviceDb me = db.selector(DeviceDb.class).where("macAddr", "=", address).findFirst();
            if (me != null) {
                me.setBrightness(brightness);
                db.saveOrUpdate(me);
            } else {
                Log.i(TAG, "setYeeBright: Not found, MAC:" + address);
            }

        } catch (DbException e) {

            e.printStackTrace();

        } catch (NumberFormatException e) {

            e.printStackTrace();
        }

    }

}
